//
//  SecondTableViewCell.m
//  On The Load
//
//  Created by 许嘉成 on 15/11/14.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "SecondTableViewCell.h"
#import "CoreDataManager.h"
#import "XJCMusciModel.h"

@interface SecondTableViewCell ()


@property (nonatomic, strong) UIButton *shareBtn;

@end


@implementation SecondTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
    }
    return self;
}
-(void)createCell
{
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH / 2, 5, 1, 40 * SCALEH)];
    lineView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    [self.contentView addSubview:lineView];
    
    self.collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.collectBtn.frame = CGRectMake(0, 0, 25 * SCALEW, 25 * SCALEW);
    self.collectBtn.center = CGPointMake(WIDTH / 4, 25 * SCALEH);
    self.collectBtn.selected = NO;
    [self.collectBtn setImage:[UIImage imageNamed:@"iconfont-xihuan-2.png"] forState:UIControlStateNormal];
    [self.collectBtn addTarget:self action:@selector(collectAction) forControlEvents:UIControlEventTouchUpInside];
    [self.collectBtn setImage:[UIImage imageNamed:@"iconfont-xihuan-3.png"] forState:UIControlStateSelected];
    [self.contentView addSubview:self.collectBtn];
    
    self.shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shareBtn.frame = CGRectMake(0, 0, 25 * SCALEW, 25 * SCALEW);
    self.shareBtn.center = CGPointMake(WIDTH * 3 / 4, 25 * SCALEH);
    self.shareBtn.selected = NO;
    [self.shareBtn setImage:[UIImage imageNamed:@"iconfont-share.png"] forState:UIControlStateNormal];
    [self.shareBtn addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.shareBtn];
}
- (void)collectAction{
    
    [self.collectDelegate collectMusic:self.collectBtn];
}
- (void)shareAction{
    [self.shareDelegate shareMusic];
}
-(void)setMusicM:(XJCMusciModel *)musicM
{
    _musicM = musicM;
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"MusicModel"];
    request.predicate = [NSPredicate predicateWithFormat:@"name = %@", musicM.name];
    
    //向数据管理器发送请求
    NSArray *array = [[CoreDataManager shareManager].managedObjectContext executeFetchRequest:request error:nil];
    
    if (array.count > 0) {
        self.collectBtn.selected = YES;
    }else{
        self.collectBtn.selected = NO;
    }
    
}

@end
